package org.rency.ues.annotation.advice;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.rency.ues.annotation.Decrypt;
import org.rency.ues.annotation.service.DecryptService;
import org.rency.ues.annotation.support.AnnotionData;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

/**
 * 解密切入点
 * @author: user_rcy@163.com
 * </br>Created on 2016年7月22日
 * @version: V1.0.0
 * @since JDK 1.8
 */
@Component
@Aspect
public class DecryptAdvice extends UesAdviceBase {
    
	@Resource(name="decryptService")
	private DecryptService decryptService;

    @Pointcut("@annotation(org.rency.ues.annotation.Decrypt)")
    public void decrypt() {
    }

    @Around("decrypt()")
    public Object aroundOutSecurityManagement(ProceedingJoinPoint pjp) throws Throwable {
        Object rest = pjp.proceed();
        if (rest == null) {
            return null;
        }
        Method currentMethod = this.getCurrentMethod(pjp);
        Annotation annotation = this.getMethodAnnotation(currentMethod, Decrypt.class);
        AnnotionData ad = this.buildDecryptAnnotationData(pjp, currentMethod, annotation, rest);
        Object newRest = decryptService.process(rest, ad);
        return newRest;
    }

}
